#!/usr/bin/env python

import os
import re

problem = "ctp3"
extra_parameters = "-d 0"
task_number = 14

microtask_number = 1
microtask_file = problem + "_mutask" + str(task_number) + "_"

tasks = [
    ## 10 vertices
    #'small-graphs/test00_10.graph',
    #'small-graphs/test01_10.graph',
    #'small-graphs/test02_10.graph',
    #'small-graphs/test03_10.graph',
    #'small-graphs/test04_10.graph',
    #'small-graphs/test05_10.graph',
    #'small-graphs/test06_10.graph',
    #'small-graphs/test07_10.graph',
    #'small-graphs/test08_10.graph',
    #'small-graphs/test09_10.graph',
    ## 20 vertices
    #'AAAI-graphs/test00_20_T.graph',
    #'AAAI-graphs/test01_20_T.graph',
    #'AAAI-graphs/test02_20_T.graph',
    #'AAAI-graphs/test03_20_T.graph',
    #'AAAI-graphs/test04_20_T.graph',
    'AAAI-graphs/test05_20_T.graph',
    #'AAAI-graphs/test06_20_T.graph',
    #'AAAI-graphs/test07_20_T.graph',
    #'AAAI-graphs/test08_20_T.graph',
    #'AAAI-graphs/test09_20_T.graph',
]

policies = [
    #"random uct",
    #"optimistic uct",
    #"random aot",
    #"optimistic aot",
    #"optimistic aot-random",
    #"random aot-random",
    "greedy aot-value", # h = zero
    "optimistic aot-value", # h = min-min
]

rollout_widths = [ 10, 100 ]
uct_widths = [ 10, 50, 100, 500, 1000, 5000, 10000 ]
uct_parameters = [ 0 ]
aot_widths = [ 10, 50, 100, 500, 1000, 5000, 10000 ]
aot_random_widths = [ 5000, 10000 ]
aot_value_widths = [ 10, 50, 100, 500, 1000, 5000, 10000 ]
aot_parameters = [ 0.5 ]
aot_nexp = [ .1 ]

def open_file():
    f = open(microtask_file + str(microtask_number), 'w')
    f.write('#!/usr/bin/env python\n')
    f.write('import os\n')
    f.write('output = open("output.' + microtask_file + str(microtask_number) + '", "w")\n')
    f.write('cmd = "./' + problem + ' -h 1 -s 0 ' + extra_parameters + ' ')
    return f

def close_file(f):
    global microtask_number
    f.write('"\n')
    f.write('output.write("cmd=" + cmd + "\\n")\n')
    f.write('output.close()\n')
    f.write('os.system(cmd + " >> output.' + microtask_file + str(microtask_number) + '")\n')
    f.close()
    microtask_number += 1

for task in tasks:
    base = os.path.basename(task)
    depth = re.split("[_\.]", base)[1]
    for p in policies:
        if p == "optimal direct" or p == "greedy direct" or p == "random direct":
            f = open_file()
            f.write(task + ' ' + p)
            close_file(f)
        elif p == "greedy rollout" or p == "random rollout" or p == "optimistic rollout":
            for width in rollout_widths:
                for nesting in range(1, 2):
                    f = open_file()
                    f.write(task + ' ' + p + ' ')
                    f.write(str(width) + ' ' + str(depth) + ' 0 ' + str(nesting))
                    close_file(f)
        elif p == "random uct" or p == "greedy uct" or p == "optimistic uct":
            for width in uct_widths:
                for par in uct_parameters:
                    f = open_file()
                    f.write(task + ' ' + p + ' ')
                    f.write(str(width) + ' ' + str(depth) + ' ' + str(par))
                    close_file(f)
        elif p == "random aot" or p == "greedy aot" or p == "optimistic aot":
            for width in aot_widths:
                for par in aot_parameters:
                    for factor in aot_nexp:
                        nexp = int(factor * width)
                        if nexp > 0:
                            f = open_file()
                            f.write(task + ' ' + p + ' ')
                            f.write(str(width) + ' ' + str(depth) + ' ' + str(par) + ' ' + str(nexp))
                            close_file(f)
        elif p == "random aot*" or p == "greedy aot*" or p == "optimistic aot*":
            for width in aot_widths:
                for par in aot_parameters:
                    for factor in aot_nexp:
                        nexp = int(factor * width)
                        if nexp > 0:
                            f = open_file()
                            f.write(task + ' ' + p + ' ')
                            f.write(str(width) + ' ' + str(depth) + ' ' + str(par) + ' ' + str(nexp))
                            close_file(f)
        elif p == "random aot-random" or p == "optimistic aot-random":
            for width in aot_random_widths:
                for par in aot_parameters:
                    for factor in aot_nexp:
                        nexp = int(factor * width)
                        if nexp > 0:
                            f = open_file()
                            f.write(task + ' ' + p + ' ')
                            f.write(str(width) + ' ' + str(depth) + ' ' + str(par) + ' ' + str(nexp))
                            close_file(f)
        elif p == "greedy aot-value" or p == "optimistic aot-value":
            for width in aot_value_widths:
                for par in aot_parameters:
                    for factor in aot_nexp:
                        nexp = int(factor * width)
                        if nexp > 0:
                            f = open_file()
                            if p == "greedy aot-value":
                                f.write('-h 10 ')
                            else:
                                f.write('-h 12 ')
                            f.write(task + ' ' + p + ' ')
                            f.write(str(width) + ' ' + str(depth) + ' ' + str(par) + ' ' + str(nexp))
                            close_file(f)

